package com.ey12.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.TimerTask;
import com.ey12.utils.DateUtil;
import java.util.Timer;

public class CheckTask extends TimerTask {

	private static String URL = "";
	private static String USN = "";
	private static String PWD = "";
	private static int timerInterval = 3;
	private static int checkInterval = 24;
	private static int quickInterval = 7;

	public CheckTask(int checkInterval, int quickInterval, String URL,
			String USN, String PWD) {
		this.checkInterval = checkInterval;
		this.quickInterval = quickInterval;
		this.URL = URL;
		this.USN = USN;
		this.PWD = PWD;
	}

	public void run() {
		System.out.println("[" + new DateUtil().toTimeString()
				+ "] The CheckTask is running...");
		findOrders();
		cancelOrders();
	}

	// update gm_order set state=2 where pay_state=0 and state<2 and
	// rec_state!=1 and create_date<=SUBDATE(NOW(), INTERVAL 30 minute)

	public void cancelOrders() {
		Connection conn = getConnectionByJDBC();
		String sqlx = "update gm_order set state=2 where  pay_state=0 and  state<2 and rec_state!=1 and  create_date<=SUBDATE(NOW(), INTERVAL "
				+ quickInterval + " minute)";
		System.out.println("cancelOrders=" + sqlx);
		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sqlx);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {

			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
	}

	// select * from gm_order where pay_state=1 and state=3 and
	// send_date<=SUBDATE(NOW(), INTERVAL 24 hour)

	public void findOrders() {
		Connection conn = getConnectionByJDBC();
		String sqlx = "select id,user_id,fact_money,store_id from gm_order where  pay_state=1 and  state=3 and rec_state=0 and  send_date<=SUBDATE(NOW(), INTERVAL "
				+ checkInterval + " hour)";
		System.out.println("findOrders=" + sqlx);
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				int id = rs.getInt("id");
				updateOrder(id);
				int user_id = rs.getInt("user_id");
				int store_id = rs.getInt("store_id");

				double fact_money = rs.getDouble("fact_money");
				updateUser(user_id, fact_money);
				updateSellUser(id, store_id, fact_money);
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {

			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
	}

	public void updateOrder(int id) {
		// update gm_order set rec_state=1,rec_date=''
		Connection conn = getConnectionByJDBC();
		String sqlx = "update gm_order set rec_state=1,rec_date='"
				+ new DateUtil().toTimeString() + "' where id=" + id;
		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sqlx);
			System.out.println("updateOrder=" + sqlx);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}

	}

	public int findStoreById(int paramid) {
		Connection conn = getConnectionByJDBC();
		String sqlx = "select user_id from gm_game_store where id=" + paramid;
		System.out.println("findStoreById=" + sqlx);
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				int id = rs.getInt("user_id");
				return id;
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {

			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		return -1;
	}

	public int findMemberLevelIdByUID(int paramid) {
		Connection conn = getConnectionByJDBC();
		String sqlx = "select sell_level_num from gm_user where id=" + paramid;
		System.out.println("findMemberLevelIdByUID=" + sqlx);
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				int sell_level_num = rs.getInt("sell_level_num");
				return sell_level_num;
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {

			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		return -1;
	}

	public double findWebGetByID(int paramid) {
		Connection conn = getConnectionByJDBC();
		String sqlx = "select web_get from gm_member_level where level_num="
				+ paramid;
		System.out.println("findWebGetByID=" + sqlx);
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				double web_get = rs.getDouble("web_get");
				return web_get;
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {

			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		return -1;
	}

	public void updateSellUser(int orderid, int store_id, double money) {
		int sellid = findStoreById(store_id);
		int sell_level_num = findMemberLevelIdByUID(sellid);
		double web_get = findWebGetByID(sell_level_num);
		if (sellid != -1) {
			if (sell_level_num != -1) {
				if (web_get != -1) {
					java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); 
					double realmoney=Double.valueOf(df.format(money*(1-web_get))).doubleValue();
					Connection conn = getConnectionByJDBC();
					String sqlx = "update gm_user set count_balance=count_balance+"
							+ realmoney + " where id=" + sellid;

					try {
						Statement stmt = conn.createStatement();
						stmt.execute(sqlx);
						System.out.println("updateSellUser=" + sqlx);
					} catch (SQLException e) {
						System.out.println(e.getMessage());
						e.printStackTrace();
					} finally {
						try {
							if (conn != null)
								conn.close();
						} catch (SQLException e) {
							System.out.println(e.getMessage());
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

	public static void main(String[] a) {
		java.text.DecimalFormat df = new java.text.DecimalFormat("#");
		System.out.println("DecimalFormat=" + df.format(2));
	}

	public void updateUser(int userid, double money) {
		java.text.DecimalFormat df = new java.text.DecimalFormat("#");
		Connection conn = getConnectionByJDBC();
		String sqlx = "update gm_user set point=point+" + df.format(money)
				+ " where id=" + userid;

		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sqlx);
			System.out.println("updateUser=" + sqlx);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}

	}

	public static Connection getConnectionByJDBC() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(URL, USN, PWD);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return conn;
	}
}
